package com.thoughtworks.turtlemock.filemover;

import com.thoughtworks.turtlemock.Mock;
import com.thoughtworks.turtlemock.Turtle;
import junit.framework.TestCase;

import java.io.File;

public class FileMoverTurtleMockTest extends TestCase {
    private FileAdaptee file;
    private Mock fileMock;
    private FileMover fileMover;
    private FileAdaptee destDir;
    private Mock destFileMock;

    protected void setUp() throws Exception {
        Mock destDirMock = Turtle.mock(FileAdaptee.class);
        destDirMock.ifCall("getAbsolutePath").willReturn("destination");
        destDir = (FileAdaptee) destDirMock.mockTarget();

        fileMover = new FileMover(destDir);

        destFileMock = Turtle.mock(FileAdaptee.class);
        fileMock = Turtle.mock(FileAdaptee.class);
        fileMock.ifCall("getName").willReturn("fileName.ext");
        fileMock.ifCall("moveTo").willReturn(destFileMock.mockTarget());
        fileMock.ifCall("getAbsolutePath").willReturn("file absolute path");

        file = (FileAdaptee) fileMock.mockTarget();
    }

    public void testShouldMoveFileToTargetDir() throws Exception {
        fileMover.move(file);
        fileMock.assertDid("moveTo").with(destDir.getAbsolutePath() + File.separator + file.getName());
    }

    public void testShouldBeRollbackable() throws Exception {
        fileMover.move(file);
        fileMover.rollback();

        destFileMock.assertDid("moveTo").with(file.getAbsolutePath());
    }

    public void testShouldRollbackAllMovingActions() throws Exception {
        Mock anotherDestFileMock = Turtle.mock(FileAdaptee.class);
        Mock anotherFileMock = Turtle.mock(FileAdaptee.class);
        anotherFileMock.ifCall("getName").willReturn("anotherFileName.ext");
        anotherFileMock.ifCall("getAbsolutePath").willReturn("anotherFile absolute path");
        anotherFileMock.ifCall("moveTo").willReturn(anotherDestFileMock.mockTarget());

        FileAdaptee anotherFile = (FileAdaptee) anotherFileMock.mockTarget();

        fileMover.move(file);
        fileMover.move((FileAdaptee) anotherFileMock.mockTarget());
        fileMover.rollback();

        destFileMock.assertDid("moveTo").with(file.getAbsolutePath());
        anotherDestFileMock.assertDid("moveTo").with(anotherFile.getAbsolutePath());
    }

}
